package com.example.login.Controller;

import com.example.login.Mapper.GoodsMap;
import com.example.login.Mapper.SalesMap;
import com.example.login.Pojo.Goods;
import com.example.login.Pojo.Sales;
import com.example.login.Pojo.Seller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;

@Controller
public class SaleController {
    @Autowired
    SalesMap salesMap;
    @Autowired
    GoodsMap goodsMap;
    public String insertSale(String starttime,String endtime,int saletype,float first_account,int last_account){
        if(saletype==0)
            salesMap.insertSale(starttime,endtime,saletype,first_account,0);
        else
            salesMap.insertSale(starttime,endtime,saletype,first_account,last_account);
        return "";
    }
    public String getAccount(int goods_id,int price){
        int sale_id=salesMap.getSaleIdByGoodsId(goods_id);
        Sales sales=salesMap.getSaleById(sale_id);
        float finPrice;
        if(sales.getSaletype()==0)
             finPrice=price*sales.getLast_account();
        else
             finPrice=price-(price%sales.getFirst_account())*sales.getLast_account();
        return "";
    }
    @RequestMapping("/merchant/Discount")
    public String getAccountPage( RedirectAttributes attributes,
                                 HttpSession session){
        Seller seller = (Seller)session.getAttribute("seller");
        int seller_id=seller.getId();
        ArrayList<Goods> goodsArrayList=goodsMap.getGoodsList(seller_id);
        ArrayList<ArrayList<Goods>> goodsList=new ArrayList<>();
        ArrayList<Goods> temp=new ArrayList<>(3);
        for(int i=0;i<goodsArrayList.size();i++){
             temp.add(goodsArrayList.get(i));
             if (i%3==2){
                 goodsList.add(temp);
                 temp=new ArrayList<>(3);
             }
        }
        attributes.addFlashAttribute("goods",goodsList);
        return "redirect:/youhui";
    }
}
